<%--                                                                       --%>
<%-- Copyright (c) 2005 BroadVision, Inc.  All rights reserved.            --%>
<%--                                                                       --%>
<%-- This software is copyrighted. Under the copyright laws, this software --%>
<%-- may not be copied, in whole or in part, without prior written consent --%>
<%-- of BroadVision, Inc. or its assignees. This software is provided      --%>
<%-- under the terms of a license between BroadVision and the recipient,   --%>
<%-- and its use is subject to the terms of that license.                  --%>
<%--                                                                       --%>
<%-- This software may be protected by one or more U.S. and International  --%>
<%-- patents.  Certain applications of BroadVision One-To-One software are --%>
<%-- covered by U.S. patent 5,710,887.                                     --%>
<%--                                                                       --%>
<%-- TRADEMARKS: BroadVision and BroadVision One-To-One are registered     --%>
<%-- trademarks of BroadVision, Inc., in the United States and the         --%>
<%-- European Community, and are trademarks of BroadVision, Inc., in other --%>
<%-- countries.  The BroadVision logo, is a trademark of BroadVision,      --%>
<%-- Inc., in the United States and other countries. Additionally, IONA    --%>
<%-- and Orbix are trademarks of IONA Technologies, Ltd.  RSA, MD5, and    --%>
<%-- RC2 are trademarks of RSA Data Security, Inc.                         --%>
<%--                                                                       --%>

<%@ taglib uri="/WEB-INF/c.tld" prefix="c" %>
<%@ taglib uri="/WEB-INF/struts-html-el.tld" prefix="html-el" %>
<%@ taglib uri="/WEB-INF/struts-bean-el.tld" prefix="bean-el" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>

<%
  String ctxPath = request.getContextPath();
%>

<c:set var="order"><bean-el:message key="ep.header.order" bundle="ep.messages"/></c:set>

<script language="JavaScript">

//
//
//
function validateForm(form) {
<c:forEach var="item" items="${programs}" varStatus="index">
  if (!isValidOrder(form, "order<c:out value="${index.count}"/>")) return false;
</c:forEach>

  return true;
}

//
//
//
function isValidOrder(form, name) {
  var field = form[name];
  if (!isValidInteger(field.value)) {
    field.focus();
    alert("<bean-el:message key="errors.integer" arg0="${order}"/>");
    return false;
  }
  return true;
}

//
//
//
function isValidInteger(value) {
  var bValid = true;
  if (value.length > 0) {
    if (!isAllDigits(value)) {
      bValid = false;
    }
    else {
      var iValue = parseInt(value);
      if (isNaN(iValue) || !(iValue >= -2147483648 && iValue <= 2147483647)) {
        bValid = false;
      }
    }
  }
  return bValid;
}

<bean:size id="numPrograms" name="programs"/>

//
//
//
function doMoveBottom(srcIndex) {
  var form = document.epOrderForm;

  var name = "order" + srcIndex;
  var expr = "var field = form['" + name + "']";
  eval(expr);
  field.value = <c:out value="${numPrograms}"/>;

  for (var index = srcIndex + 1; index <= <c:out value="${numPrograms}"/>; index++) {
    name = "order" + index;
    expr = "field = form['" + name + "']";
    eval(expr);
    field.value = index - 1;
  }

  form.submit();
}

//
//
//
function doMoveDown(srcIndex) {
  var form = document.epOrderForm;

  var destIndex = srcIndex + 1;
  var srcName = "order" + srcIndex;
  var destName = "order" + destIndex;

  form.reset();
  doSwapOrder(srcName, destName);
  form.submit();
}

//
//
//
function doMoveUp(srcIndex) {
  var form = document.epOrderForm;

  var destIndex = srcIndex - 1;
  var srcName = "order" + srcIndex;
  var destName = "order" + destIndex;

  form.reset();
  doSwapOrder(srcName, destName);
  form.submit();
}

//
//
//
function doMoveTop(srcIndex) {
  var form = document.epOrderForm;

  var name = "order" + srcIndex;
  var expr = "var field = form['" + name + "']";
  eval(expr);
  field.value = 1;

  for (var index = 1; index < srcIndex; index++) {
    name = "order" + index;
    expr = "field = form['" + name + "']";
    eval(expr);
    field.value = index + 1;
  }

  form.submit();
}

//
//
//
function doSwapOrder(srcName, destName) {
  var form = document.epOrderForm;
  var expr = "var srcField = form['" + srcName + "']";
  eval(expr);

  var expr = "var destField = form['" + destName + "']";
  eval(expr);

  var tmp = destField.value;

  destField.value = srcField.value;
  srcField.value = tmp;
}

//
//
//
function doDone(form) {
  if (!validateForm(form)) return false;

  form.action = '<c:url value="/ep/programOrderSave.do"/>';
  form.submit();
}

//
//
//
function doCancel() {
  window.close();
}

</script>

<c:set var="moveupImg">
<img src="<%= ctxPath %>/mc/images/action/icon_moveup.gif" width="15" height="15" border="0" class="actionIcon" alt="<bean-el:message key="image.alt.moveUp" bundle="mc.messages"/>" title="<bean-el:message key="image.alt.moveUp" bundle="mc.messages"/>">
</c:set>
<c:set var="movedownImg">
<img src="<%= ctxPath %>/mc/images/action/icon_movedown.gif" width="15" height="15" border="0" class="actionIcon" alt="<bean-el:message key="image.alt.moveDown" bundle="mc.messages"/>" title="<bean-el:message key="image.alt.moveDown" bundle="mc.messages"/>">
</c:set>
<c:set var="movetopImg">
<img src="<%= ctxPath %>/mc/images/action/icon_movetop.gif" width="15" height="15" border="0" class="actionIcon" alt="<bean-el:message key="image.alt.moveTop" bundle="mc.messages"/>" title="<bean-el:message key="image.alt.moveTop" bundle="mc.messages"/>">
</c:set>
<c:set var="movebottomImg">
<img src="<%= ctxPath %>/mc/images/action/icon_movebottom.gif" width="15" height="15" border="0" class="actionIcon" alt="<bean-el:message key="image.alt.moveBottom" bundle="mc.messages"/>" title="<bean-el:message key="image.alt.moveBottom" bundle="mc.messages"/>">
</c:set>
<c:set var="spaceImg">
<img src="<%= ctxPath %>/mc/images/build/transparent.gif" width="15" height="15" border="0" alt="" title="">
</c:set>

<html-el:javascript formName="epChannelForm" method="validate"/>

<form name="epOrderForm" action="<c:url value="/ep/programOrderPreview.do"/>" method="post">

<input type="hidden" name="channelId" value="<c:out value="${epChannelView.map.id}"/>"/>

<table class="zoneFive" width="100%" border="0" cellspacing="0" cellpadding="0" summary="content area">
  <tr>
    <td valign="top" width="100%">

    <div class="generalWrapRight"> 
        <input class="inputButton" onClick="doCancel()" type="button" value="<bean-el:message key="button.close" bundle="ep.messages"/>">
    </div>


        <div class="pageSubtitle">
          <span class="left"><bean-el:message key="ep.program.order.subtitle" bundle="ep.messages"/></span>

          <span class="right">&nbsp;</span>
        </div>

        <div class="pageInstructions">
          <bean-el:message key="ep.program.order.instructions" bundle="ep.messages"/>
        </div>

      <div class="globalButtonWrap">
        <input class="globalButton" type="submit" value="<bean-el:message key="button.reorder" bundle="ep.messages"/>" onclick="return validateForm(this.form)">
      </div>

        <table class="dataTable" border="0" cellspacing="1" cellpadding="2" summary="Search result table" width="100%">
          <tr class="listHeader">
            <th id="Order" align="center" width="10%"><bean-el:message key="ep.program.order.header.order" bundle="ep.messages"/></th>
            <th id="Name" align="left" width="75%"><bean-el:message key="ep.program" bundle="ep.messages"/></th>
            <th id="Actions" align="left" width="15%" nowrap><bean-el:message key="ep.header.actions" bundle="ep.messages"/></th>
          </tr>

          <!-- results -->

<c:forEach var="item" items="${programs}" varStatus="index">
          <c:choose>
            <c:when test="${(index.count + 1) % 2 == 0}">
              <tr class="listEven">
            </c:when>
            <c:otherwise>
              <tr class="listOdd">
            </c:otherwise>
          </c:choose>
            <td headers="Order" align="center"><input type="text" name="order<c:out value="${index.count}"/>" size="3" maxlength="3" value="<c:out value="${index.count}"/>">
              <input type="hidden" name="orderId.<c:out value="${index.count}"/>" value="<c:out value="${item.map.id}"/>"></td>
            <td headers="Name"><c:out value="${item.map.name}"/></td>
            <td headers="Actions">
  <c:if test="${index.count < numPrograms}">
              <a href="#" onclick="doMoveBottom(<c:out value="${index.count}"/>)"><c:out value='${movebottomImg}' escapeXml="false"/></a>
              <a href="#" onclick="doMoveDown(<c:out value="${index.count}"/>)"><c:out value='${movedownImg}' escapeXml="false"/></a>
  </c:if>
  <c:if test="${index.count == numPrograms}">
              <c:out value='${spaceImg}' escapeXml="false"/>
              <c:out value='${spaceImg}' escapeXml="false"/>
  </c:if>
  <c:if test="${index.count > 1}">
              <a href="#" onclick="doMoveUp(<c:out value="${index.count}"/>)"><c:out value='${moveupImg}' escapeXml="false"/></a>
              <a href="#" onclick="doMoveTop(<c:out value="${index.count}"/>)"><c:out value='${movetopImg}' escapeXml="false"/></a>
  </c:if>
            </td>
          </tr>
</c:forEach>

        </table>

      <div class="pageButtonWrap" align="right">
        <input type="reset" name="resetBtn" value="<bean-el:message key='button.reset' bundle="ep.messages"/>" class="inputButton">
        &nbsp;&nbsp;
        <input class="inputButton" onclick="doCancel()" type=button value="<bean-el:message key='button.cancel' bundle="ep.messages"/>">
        <input class="inputButton" onclick="doDone(this.form)" type="button" value="<bean-el:message key='button.done' bundle="ep.messages"/>">         
      </div>

    </td>
  </tr>
</table>

</form>
